package pprg.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JPanel;

import pprg.core.GameOfLife;

public class GameOfLifePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private GameOfLife gol;
	boolean[][] array;

	public GameOfLifePanel(GameOfLife gol) {
		this.gol = gol;
		array = gol.getArray();
		addListener();
	}

	private void addListener() {

		MouseMotionAdapter mml = new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				int x = (int) (e.getX() / (getSize().getWidth() / array.length));
				int y = (int) (e.getY() / (getSize().getHeight() / array.length));
				if (x < array.length && y < array.length && x >= 0 && y >= 0) {
					if (array[y][x] == false) {
						array[y][x] = true;
					}
					gol.setArray(array);
					repaint();
				}
			}
		};

		MouseAdapter ml = new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				int x = (int) (e.getX() / (getSize().getWidth() / array.length));
				int y = (int) (e.getY() / (getSize().getHeight() / array.length));

				if (x < array.length && y < array.length && x >= 0 && y >= 0) {
					if (array[y][x] == false) {
						array[y][x] = true;
					} else {
						array[y][x] = false;
					}
					gol.setArray(array);
					repaint();
				}
			}
		};
		addMouseMotionListener(mml);
		addMouseListener(ml);

		ComponentAdapter golSizeListener = new ComponentAdapter() {

			@Override
			public void componentResized(ComponentEvent e) {
				int width = (int) Math.floor(getWidth());
				int height = (int) Math.floor(getHeight());
				if (array != null) {
					Dimension newSize = new Dimension(array.length
							* (width / array.length), array.length
							* (height / array.length));
					setSize(newSize);
				}
			}

		};
		addComponentListener(golSizeListener);

	}

	public void paintComponent(Graphics gr) {
		
		array = gol.getArray();

		Graphics2D g = (Graphics2D) gr;
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		int width = (int) Math.floor(g.getClipBounds().getWidth());
		int height = (int) Math.floor(g.getClipBounds().getHeight());

		if (array != null) {
			int length = array.length;

			for (int y = 0; y < length; y++) {
				for (int x = 0; x < length; x++) {
					if (array[y][x] == true) {
						g.setColor(Color.green);
					} else {
						g.setColor(Color.black);
					}
					g.fillRect((width / length) * x, (height / length) * y,
							width / length, height / length);
				}
			}
			g.setColor(Color.darkGray);

			for (int x = 0; x < width; x += width / length)
				g.drawLine(x, 0, x, height);
			for (int y = 0; y < height; y += height / length)
				g.drawLine(0, y, width, y);

		}
	}

	public void setArray(boolean[][] array) {
		this.array = array;
	}
}
